Ollama
¶
Ollama is an open-source project that serves as a powerful and user-friendly platform for running LLMs on your local machine. So, it lets you download and run large language models (LLMs) on your own machine, without relying on cloud-hosted services. Ollama server is running in the docker and needs to import the python library to work with ollama.
import ollama
Choose your model. The current downloaded model in the docker is gemma3:1b, which consists of 1B parameters and has size of 815MB
model_name="gemma3:1b"
# model_name = "deepseek-r1"
Continuous chat in Ollama¶
To maintain a conversational history where the model's response depends on all previous questions and answers, you need to store coversation history in a list of messages
history = []
def ask_ollama(question):
#add new message to history
history.append({'role': 'user', 'content': question})
#send full history to ollama
response = ollama.chat(model=model_name, messages=history)
#add assistant response to history
history.append({'role': 'assistant', 'content': response['message']['content']})
return response['message']['content']
Create an interface for Ollama¶
ipywidgets is a great way to build an interactive, conversational tool. The core idea is to combine a text area for user input, a button to trigger the action, and an output widget to display the response.
import ipywidgets as widgets
from IPython.display import display
Widget Creation¶
Next, create the individual widgets. The Textarea is perfect for multi-line user input. The Button will serve as the "send" or "submit" control. Finally, the Output widget is where the responses will be displayed.
# Create the input text area
user_input = widgets.Textarea(
value='',
placeholder='Ask me anything...',
description='Your Query:',
disabled=False,
layout=widgets.Layout(width='auto', height='100px')
)
# Create the send button
send_button = widgets.Button(
description='Send',
button_style='success',
tooltip='Click to get a response',
icon='paper-plane'
)
# Create the output area
output_area = widgets.Output()
Logic and Callback Function¶
The core of the functionality is a callback function that executes when the button is clicked. This function will take the text from the user_input widget, process it using ask_ollama() function, and then display the result in the output_area.
def on_button_clicked(b):
with output_area:
output_area.clear_output()
query = user_input.value
if query:
print(f"You asked: {query}")
# Response from Ollama running model
response = ask_ollama(query)
print(response)
else:
print("Please enter a query.")
user_input.value = '' # Clear the input after sending
Now, link the button to this function using on_click().
send_button.on_click(on_button_clicked)
Layout and Display¶
Finally, arrange the widgets in a logical layout. A vertical box (VBox) is a good choice for stacking the input, button, and output areas.
# Create the layout
chat_gui = widgets.VBox([user_input, send_button, output_area])
# Display the entire GUI
display(chat_gui)
VBox(children=(Textarea(value='', description='Your Query:', layout=Layout(height='100px', width='auto'), plac…